import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Login from '../views/login/index.vue'
import Preview from '../views/preview/index.vue'
import { getaccessToken } from '@/utils/index'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'view',
    meta: {
      title: "登录",
      requireAuth: false
    },
    component: Login
  },
  {
    path: '/develop',
    name: 'develop',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "develop" */ '../views/develop/index.vue')
  },
  {
    path: '/preview',
    name: 'preview',
    component: Preview
  },
  {
    path: "/home",
    name: "首页",
    component: () => import(/* webpackChunkName: "home" */ '../views/home/index.vue'),
    // children: [
    //   {
    //     path: "/home",
    //     name: "首页",
    //     component: () => import(/* webpackChunkName: "welcome" */ "../views/common/welcome.vue")
    //   }
    // ]
  },
  {
    path: '/zip',
    name: 'zip',
    component: () => import(/* webpackChunkName: "zip" */ "../views/zip/index.vue")
  },
  {
    path: '/changeWord',
    name: 'changeWord',
    component: () => import(/* webpackChunkName: "changeWord" */ "../views/changeWord/index.vue")
  },
  {
    path: "/setting",
    name: "setting",
    component: () => import(/* webpackChunkName: "changeWord" */ "../views/setting/index.vue")
  },
  {
    path: '/:catchAll(.*)',
    redirect: '/404'
  },
  {
    path: "/404",
    component: () => import(/* webpackChunkName: "404" */ "../views/common/404.vue")
  }
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.requireAuth !== false) {
    if (getaccessToken()) {
      next()
    } else {
      window.location.href = "/"
    }
  } else {
    next()
  }
  // next()
})

export default router
